--
-- Author: shunguo.chen
-- Date: 2018-11-07 18:41:32
--
-- TeamMatchController 组队查找器控制器
--

local TeamMatchView = import("..views.TeamMatchView")
local NewTeamBossInfoView = import("..views.NewTeamBossInfoView")
local NewSpeedDungeonsBookView = import("..views.NewSpeedDungeonsBookView")
local NewWorldBossInfoView = import("..views.NewWorldBossInfoView")
local NewWildBossInfoView = import("..views.NewWildBossInfoView")
local NewActivityDungeonInfoView = import("..views.NewActivityDungeonInfoView")

local NewSpeedDungeonsWinView = import("..views.NewSpeedDungeonsWinView")

local TeamRecruitInfoVo = import("..models.TeamRecruitInfoVo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("TeamModel")

    -- 当前打开的面板
    self._curIdx = 1
    -- 缓存当前打开的id
    self._curTargetIds = {}

    -- 好运符现在不用了
    -- self._playerHandler = UD.player:onChanged(handler(self, self.updatePlayer))
    self._walletHandler = UD:onChanged("wallet", handler(self, self.updateWallet))
    self._dungeonTimesHandler = UD:onChanged("dungeonTimes", handler(self._model, self._model.onUpdateDungeonTimes))
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)

    -- UD.player:unChanged(self._playerHandler)
    UD:unChanged("wallet", self._walletHandler)
    UD:unChanged("wallet", self._dungeonTimesHandler)
end

function ClassRef:listEvent()
    return {
        "onDailyUpdate",
        "onMapChanged",
        "onCreateTeam",
        "onJoinTeam",
        "onTeamUpdate",
        "onTeamMemberUpdate",
        "onTeamLeaderChanged",
        "onTeamSettingUpdate",
        "onTeamNewApplyer",
        "onLoginedNoticeOver",
    }
end

function ClassRef:onEvent(eventName, data)
    if eventName == "onDailyUpdate" then
        self._model:onDailyUpdate(data)
    elseif eventName == "onMapChanged" then
        if data then
            self._model:onEnterMap()
        end
    elseif eventName == "onLoginedNoticeOver" then
        self._model:checkDungeonMatch()
    elseif eventName == "onJoinTeam" then
        -- 加入队伍
        self:updateAutoRecruit()
        self:updateAutoApply()
    elseif eventName == "onTeamUpdate" then
        -- 队伍更新
        self:refreshTeam()
    elseif eventName == "onTeamMemberUpdate" then
        -- 队友更新
        self:refreshMember()
    elseif eventName == "onTeamLeaderChanged" then
        -- 换队长
        self:refreshMember()
    elseif eventName == "onCreateTeam" then
        -- 队伍创建时，可能自动招募状态/自动申请也有改变
        self:updateAutoRecruit()
        self:updateAutoApply()
    elseif eventName == "onTeamSettingUpdate" then
        -- 队伍设置更新
        self:updateAutoRecruit()
    elseif eventName == "onTeamNewApplyer" then
         -- 来新申请人
        if self._model:isMyTeam() then
            self._model:reqGetApplicationList()
        end
        self:refreshApplyNew()
    end
end

-- 解析组队查找器打开的参数
-- data = {
--     targetId = ,     --Enums.TeamTarget 
--     matchType = ,     --Enums.TeamMatchType
--     targetType = ,     --Enums.TeamTargetType
-- }
-- 传入类型1 nil 使用默认值
-- 传入类型2 targetId 可以解析出matchType和targetType
-- 传入类型3 matchType+targetType 指定查找类型和目标类型，使用默认targetId
-- 传入类型4 matchType 指定查找类型，使用默认targetType targetId
-- 传入类型5 targetType 指定目标类型，可以解析出matchType，使用默认targetId
function ClassRef:openView(data)
    -- 副本中不能再打开组队查找器
    if DungeonManager:isInDungeon() then
        display.pushToast(L("lua_code_text_206"))
        return
    end

    -- 检测目标是否开放
    -- dump(data, "TeamMatchController.openView")
    local isOpen, param1, param2 = self._model:checkTeamMatchOpenParams(data)
    -- print("isOpen, param1, param2", isOpen, param1, param2)
    if not isOpen then
        display.pushToast(param1 or L("lua_code_text_458"))
        return
    end

    local idx = param1
    local targetId = param2
    self._curTargetIds[idx] = targetId

    if cc.isDead(self._view) then
        self._view = TeamMatchView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._view)

    local openParams = self._model:getMatchTypeOpenParams()
    self._view:show(idx, openParams)

    -- 好运钥匙
    -- self._view:refreshLuckPanel()
end
function ClassRef:clearView()
    self._model:clearPageCache()
    self._curTargetIds = {}
    if self._view then
        self._view = nil
    end
end

-- 竞速副本详情
function ClassRef:openSpeedDungeonBookView(teamTarget)
    local viewName = NewSpeedDungeonsBookView:getViewName()
    local view = UIHandler:getSystemUiByKey(viewName)
    if not view then
        view = NewSpeedDungeonsBookView.new( app.mvc.VIEWTYPE_BLUR )
        view:onCloseView(function()
            UIHandler:removeSystemUiByKey(viewName)
        end)
        view:onClearView(function()
            app:sendMsg( "PlayerController", "showExpBar", SceneZOrder.upExpBarZOrder )
        end)
    end
    UIHandler:addSystemUi(view, viewName)
    view:refresh(teamTarget)
    view:show()

    -- hide expBar
    app:sendMsg( "PlayerController", "showExpBar" )
end
function ClassRef:closeSeedDungeonBookView()
    local viewName = NewSpeedDungeonsBookView:getViewName()
    UIHandler:removeSystemUiByKey(viewName)
end

-- 地下城副本详情
function ClassRef:openDungeonBookView(teamTarget)
    local viewName = NewTeamBossInfoView:getViewName()
    local view = UIHandler:getSystemUiByKey(viewName)
    if not view then
        view = NewTeamBossInfoView.new( app.mvc.VIEWTYPE_BLUR )
        view:onCloseView(function()
            UIHandler:removeSystemUiByKey(viewName)
        end)
    end
    UIHandler:addSystemUi(view, viewName)
    view:initDungeonsBossInfo(teamTarget.id)
    view:setDungeonName(teamTarget:getFullName())
    view:show()
end
function ClassRef:closeDungeonBookView()
    local viewName = NewTeamBossInfoView:getViewName()
    UIHandler:removeSystemUiByKey(viewName)
end

-- 活动副本详情
function ClassRef:openActivityDungeonBookView(teamTarget)
    local viewName = NewActivityDungeonInfoView:getViewName()
    local view = UIHandler:getSystemUiByKey(viewName)
    if not view then
        view = NewActivityDungeonInfoView.new( app.mvc.VIEWTYPE_BLUR )
        view:onCloseView(function()
            UIHandler:removeSystemUiByKey(viewName)
        end)
    end
    UIHandler:addSystemUi(view, viewName)
    view:setDungeonName(teamTarget:getFullName())
    view:refresh(teamTarget.id)
    view:refreshBoxInfo(UD:getActivityDungeonsPoint(), UD:getLuckBoxRestTimesByType( 3 ))
    view:show()
end
function ClassRef:closeActivityDungeonBookView()
    local viewName = NewActivityDungeonInfoView:getViewName()
    UIHandler:removeSystemUiByKey(viewName)
end
function ClassRef:updateActivityDungeonBookBoxInfo()
    local viewName = NewActivityDungeonInfoView:getViewName()
    local view = UIHandler:getSystemUiByKey(viewName)
    if view then
        view:refreshBoxInfo( UD:getActivityDungeonsPoint(), UD:getLuckBoxRestTimesByType(3))
    end
end

-- 世界boss副本详情
function ClassRef:openWorldBossBookView(teamTarget)
    local viewName = NewWorldBossInfoView:getViewName()
    local view = UIHandler:getSystemUiByKey(viewName)
    if not view then
        view = NewWorldBossInfoView.new( app.mvc.VIEWTYPE_BLUR )
        view:onCloseView(function()
            UIHandler:removeSystemUiByKey(viewName)
        end)
    end
    UIHandler:addSystemUi(view, viewName)
    view:setDungeonName(teamTarget:getFullName())
    view:refresh(teamTarget.id)
    view:show()
end

-- 野外boss副本详情
function ClassRef:openWildBossBookView(teamTarget)
    local viewName = NewWildBossInfoView:getViewName()
    local view = UIHandler:getSystemUiByKey(viewName)
    if not view then
        view = NewWildBossInfoView.new( app.mvc.VIEWTYPE_BLUR )
        view:onCloseView(function()
            UIHandler:removeSystemUiByKey(viewName)
        end)
    end
    UIHandler:addSystemUi(view, viewName)
    view:setDungeonName(teamTarget:getFullName())
    view:refresh(teamTarget.id)
    view:show()
end

-- 打开战备ui
-- teamTarget     目标TeamTargetVo
-- isMatch        是否是匹配
function ClassRef:openTeamReadyView(teamTarget, isMatch)
    -- TODO 这里战备界面和查看别人的队伍设置做在一起了，或许可以优化一下
    -- 结束任务追踪
    app:getLaunchCtl():endCharacterMove()

    isMatch = isMatch or false
    local team = self._model:getTeam()

    -- 检测队伍成员最小等级是否满足副本等级要求
    local openLv = teamTarget:getOpenLv()
    local minLv = team:getMemberMinLv()
    if minLv < openLv then
        display.pushToast(string.format(L("lua_code_text_481"), openLv))
        return
    end

    -- 检测人数是否超过副本要求
    local dungeonName = teamTarget:getName()
    local memberNum = team:getMemberNum()
    local maxNum = teamTarget:getMaxNum()
    if memberNum > maxNum then
        local removeNum = memberNum - maxNum
        local msg = string.format( L("lua_code_text_309"), 
            dungeonName, maxNum, removeNum)
        local messageBox = display.newMessageBox(msg)
        UIHandler:addPopDialog( messageBox )
        return
    elseif memberNum == maxNum then
        -- 人数满了，肯定不是匹配模式
        isMatch = false
    end

    -- 灰烬入侵打开特殊界面
    if teamTarget:isGrayBattle() then
        app:sendMsg("GrayBattleController", "openView")
        return
    end

    local function openReadyView()
        -- if team:isAutoRecruit() then
        --     -- 已经开始了，取消自动招募 统一交给服务器做
        --     self:changeAutoRecruit(teamTarget, false)
        -- end

        local params = {idx=1, targetId=teamTarget.id, isMatch=isMatch}
        app:sendMsg("NewTeamReadyAndSettingController", "openView", params)
    end
    if isMatch then
        openReadyView()
    else
        if (memberNum == maxNum) and teamTarget:needCheckNum() then
            local dlg = display.newConfirmDialog( L("lua_code_text_388"),
                function() -- 确定
                    openReadyView()
                end)
            UIHandler:addPopDialog( dlg )
        else
            openReadyView()
        end
    end
end

-- 竞速副本胜利结算UI
function ClassRef:openSpeedDungeonWinView( msg )
    local view = NewSpeedDungeonsWinView.new()
    view:onCloseView(function(sender)
        if DungeonManager:isLuckBox() then -- 开宝箱
            local params = {boxType=Enums.BoxType.dungeon, dungeonID=DungeonManager:getDungeonSubId()}
            app:sendMsg("BoxController", "openView", params)
        end
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    UIHandler:addPopupUi(view, view:getViewName())

    local teamTarget = self._model:getTeamTargetById(msg.target)
    local speedInfo = teamTarget:getSpeedInfo()
    view:refresh(speedInfo, msg)
    view:show()
end

function ClassRef:openInviteFollowDialog(msg)
    local closeTime = msg.ExpiredTimeStamp
    local dialog = display.newConfirmDialog(L("TeamClumpTips1"),
            function()
                self._inviteFollowDialog = nil
                self._model:reqReplyFollow(1)
            end,
            function()
                self._inviteFollowDialog = nil
                self._model:reqReplyFollow(0)
            end)
    dialog:onClearView(function()
        self._inviteFollowDialog = nil
    end)

    UIHandler:addPopDialog(dialog,1,true)

    local btnStr = L("CommonBtnCancel") .. " (%s s)"
    local function refreshTime()
        local resTime = closeTime - math.floor(app:getServerTime())
        if resTime <= 0 then
            dialog:stopAllActions()
            self:removeInviteFollowDialog()
        else
            dialog:setCancelText(string.format(btnStr, resTime))
        end
    end

    refreshTime()
    dialog:schedule(refreshTime, 1)

    self._inviteFollowDialog = dialog
end
function ClassRef:removeInviteFollowDialog()
    if self._inviteFollowDialog then
        self._inviteFollowDialog:removeFromParent()
        self._inviteFollowDialog = nil
    end
end

-------------------------------------------------------
function ClassRef:updatePlayer(name, old, new)
    if name == "charm" then
        -- 好运钥匙更新
        -- if cc.isAlive(self._view) then
        --     self._view:refreshLuckPanel()
        -- end
    end
end

function ClassRef:updateWallet(name, old, new)
    if name == "eventPoint" then
        -- 活动点数更新
        self:updateActivityDungeonBookBoxInfo()
    end
end

-- 队伍更新
function ClassRef:refreshTeam()
    if not self:isViewAlive() then return end

    local target = self:getCurTeamTarget()
    local team = self._model:getTeam()
    self._view:refreshTeam(team)
    local isInCrossMatch = target:isInCrossMatch()
    if not isInCrossMatch and not team then
        -- 组队玩法和开放的玩法，没有队伍时需要去拉队伍
        local isSingle = target:isSingle()
        local isOpen = target:isOpen()
        local isTimeOpen, _ = target:checkTimeOpen()
        if not isSingle and isOpen and isTimeOpen then
            self._model:C2S_GetTeamList(target.id)
        else
            self:refreshTeamList({})
        end
    end
end

-- 刷新申请状态
function ClassRef:refreshApplyNew()
    if not self:isViewAlive() then return end
    self._view:refreshApplyNew(self._model:isNewApplyerFlag())
end

-- 我的队伍刷新
function ClassRef:refreshMember()
    if not self:isViewAlive() then return end

    self._view:refreshMember()
end

-- 刷新推荐队伍
function ClassRef:refreshTeamList(teamList)
    if not self:isViewAlive() then return end
    self._view:refreshTeamList(teamList)
end

-- 刷新推荐队伍里面的申请状态
function ClassRef:refreshTeamListApply(team)
    if not self:isViewAlive() then return end

    self._view:refreshTeamListApply(team)
end

-- 更新自动申请
function ClassRef:updateAutoApply()
    if not self:isViewAlive() then return end

    self._view:updateAutoApplyStatus()
end

-- 更新跨服匹配状态
function ClassRef:updateInCrossMatch()
    if not self:isViewAlive() then return end

    self._view:updateInCrossMatchStatus()
end

-- 更新自动招募
function ClassRef:updateAutoRecruit()
    if not self:isViewAlive() then return end

    self._view:updateAutoRecruitStatus()
end

-------------------------------------------------------
-- 获取当前选中的队伍目标
function ClassRef:getCurTeamTarget()
    local targetId = self._curTargetIds[self._curIdx]
    if targetId then
        return self._model:getTeamTargetById(targetId)
    end
end
-- 获取当前选中的队伍目标id
function ClassRef:getCurTeamTargetId()
    return self._curTargetIds[self._curIdx]
end

-- idx = 1 地下城
-- idx = 2 锦标赛
-- idx = 3 活动
function ClassRef:switchPanel(idx)
    self._curIdx = idx
    local targets, idxs = self._model:getMatchTargets(idx, self._curTargetIds[idx])
    self._view:refreshMenu(targets, idxs)
end

-- target TeamTarget
function ClassRef:selectTarget(target)
    self._curTargetIds[self._curIdx] = target.id

    self:refreshTeam()
end

function ClassRef:openBook(teamTarget)
    if teamTarget.id == Enums.TeamTarget.wildBoss then
        -- display.pushToast(L("lua_code_text_458"))
        self:openWildBossBookView(teamTarget)
        return
    end
    if teamTarget.id == Enums.TeamTarget.worldBoss then
        self:openWorldBossBookView(teamTarget)
        return
    end

    if teamTarget:isSpeed() then
        self:openSpeedDungeonBookView(teamTarget)
    elseif teamTarget:isPvp() then
        app:sendMsg( "PvpController", "openBookView", teamTarget)
    elseif teamTarget:isActivity() then
        self:openActivityDungeonBookView(teamTarget)
    else
        self:openDungeonBookView(teamTarget)
    end
end

function ClassRef:changeAutoRecruit(teamTarget, isAuto)
    if self._model:isInCrossMatch() then
        display.pushToast(L("CrossServerTips24"))
        self:updateAutoRecruit(teamTarget)
        return
    end

    local recruitInfo = self._model:getTeamSettingClone()
    if isAuto then
        local preTargetId = recruitInfo.targetID
        local curTargetId = teamTarget.id
        if preTargetId ~= curTargetId and preTargetId > 0 then
            local msg = string.format(L("lua_code_text_178"), teamTarget:getFullName())
            local dialog = display.newConfirmDialog(msg, 
                function()
                    local preTeamTarget = self._model:getTeamTargetById(preTargetId)
                    if preTeamTarget then
                        preTeamTarget:setAutoRecruit(false)
                    end

                    recruitInfo.targetID = curTargetId
                    recruitInfo.autoRecruit = true
                    self._model:C2S_UpdateRecruitInfo(recruitInfo)
                end,
                function()
                    -- 取消的话，刷新自动招募按钮状态
                    self:updateAutoRecruit(teamTarget)
                end)
            UIHandler:addPopDialog( dialog )
        else
            recruitInfo.targetID = curTargetId
            recruitInfo.autoRecruit = true
            self._model:C2S_UpdateRecruitInfo( recruitInfo )
        end
    else
        recruitInfo.autoRecruit = false
        recruitInfo.targetID = 0
        teamTarget:setAutoRecruit(false)
        self._model:C2S_UpdateRecruitInfo( recruitInfo ) -- 关闭“自动招募”
    end
end

-- 退出队伍
function ClassRef:leaveTeam()
    local dialog = display.newConfirmDialog( L("lua_code_text_51"),
        function() -- 确定
            self._model:reqQuitTeam()
        end)
    UIHandler:addPopDialog( dialog )
end

-- 改变队伍性质 团队／队伍
function ClassRef:changeTeamGrade()
    local team = self._model:getTeam()
    local canChangeGrade, param = team:isCanChangeGrade()
    if canChangeGrade then
        self._model:reqUpdateTeamGrade( param )
    else
        display.pushToast(param)
    end
end

-- 创建队伍
-- teamTarget TeamTargetVo
function ClassRef:createTeam(teamTarget)
    local isAutoRecruit = not teamTarget:isSingle()
    local recruitInfo = TeamRecruitInfoVo:getCreateRecruitInfo(teamTarget.id, isAutoRecruit)
    self._model:reqCreateTeam(recruitInfo)
end

-- 开始野外boss
function ClassRef:startWildBoss(itemData)
    local teamTarget = self._model:getTeamTargetById(Enums.TeamTarget.wildBoss)
    teamTarget:setWildBossData(itemData)
    if self._model:hasTeam() then
        self:teamStart(teamTarget)
    else
        self:singleStart(teamTarget)
    end
end

-- 组队开始战斗
function ClassRef:teamStart(teamTarget)
    local function checkLeader()
        if self._model:isMyTeam() then
            self:openTeamReadyView(teamTarget)
        else
            local dlg = display.newConfirmDialog( L("team_text5"),
                function() -- 确定
                    self._model:reqQuitTeam()
                end)
            UIHandler:addPopDialog( dlg )
        end
    end

    if teamTarget:isWorldBoss() then
        -- 检查是否在活动时间内
        local worldBossData = app:sendMsg("BossModel", "getWorldBossStatusData")
        if worldBossData and worldBossData.open then
            checkLeader()
        else
            display.pushToast(L("lua_code_text_249"))
        end
    else
        checkLeader()
    end
end

-- 单人开始战斗
function ClassRef:singleStart(teamTarget)
    app:sendMsg("NewTeamReadyAndSettingController", "singleStart", teamTarget)
end

-- 检测是否可以跨服匹配
function ClassRef:_checkCrossMatchEnable(teamTarget, cb)
    if teamTarget:isQuickCrossMatch() then
        -- 检测是否有跨服副本惩罚
        if UD:isDungeonCrossMatchPunish() then
            local tipStr = self._model:hasTeam() and "ErrCrossDPunishTeam" or "ErrCrossDPunishYou"
            display.pushToast(L(tipStr))
            return
        end
    end
    executeFunc(cb)
end

-- 组队开始匹配
function ClassRef:teamMatch(teamTarget)
    local function checkLeader()
        if self._model:isMyTeam() then
            self:_checkCrossMatchEnable(teamTarget, function()
                self:openTeamReadyView(teamTarget, true)
            end)
        else
            local dlg = display.newConfirmDialog( L("team_text6"),
                function() -- 确定
                    self._model:reqQuitTeam()
                end)
            UIHandler:addPopDialog( dlg )
        end
    end

    if teamTarget:isWorldBoss() then
        -- 检查是否在活动时间内
        local worldBossData = app:sendMsg("BossModel", "getWorldBossStatusData")
        if worldBossData and worldBossData.open then
            checkLeader()
        else
            display.pushToast(L("lua_code_text_249"))
        end
    else
        checkLeader()
    end
end

-- 单人开始匹配
function ClassRef:singleMatch(teamTarget)
    self:_checkCrossMatchEnable(teamTarget, function()
        app:sendMsg("NewTeamReadyAndSettingController", "singleMatch", teamTarget)
    end)
end

-- 取消匹配
function ClassRef:cancelMatch(teamTarget)
    if not self._model:isInTeam() or self._model:isMyTeam() then
        self._model:C2S_CancelCrossDungeonMatch(teamTarget.id)
    else
        local dlg = display.newConfirmDialog( L("CrossServerTips4"),
            function() -- 确定
                self._model:reqQuitTeam()
            end)
        UIHandler:addPopDialog( dlg )
    end
end


-------------------------------------------------------
local START_OPS = {
    "teamStart", 
    "singleStart", 
    "teamMatch", 
    "singleMatch", 
    "gotoWorldBoss",
    "getGrayBattleTask",
    "gotoTraining",
}
function ClassRef:teamTargetStartOp(op, ...)
    -- 跨服匹配中，检测玩法是否需要禁止
    local isInCrossMatch = self._model:isInCrossMatch()
    local teamTarget = self:getCurTeamTarget()
    if isInCrossMatch and teamTarget:isForbidInCross() then
        display.pushToast(L("CrossServerTips5"))
        return
    end

    if op == "teamStart" then
        -- 组队开始挑战 TeamTargetVo
        self:teamStart(...)
    elseif op == "singleStart" then
        -- 单人开始挑战 TeamTargetVo
        self:singleStart(...)
    elseif op == "teamMatch" then
        -- 组队开始匹配 TeamTargetVo
        self:teamMatch(...)
    elseif op == "singleMatch" then
        -- 单人开始匹配 TeamTargetVo
        self:singleMatch(...)
    elseif op == "gotoWorldBoss" then
        -- 前往挑战世界boss 
        -- 检查是否在活动时间内
        local worldBossData = app:sendMsg("BossModel", "getWorldBossStatusData")
        if worldBossData and worldBossData.open then
            self:closeView()
            app:sendMsg("WorldBossController", "closeView")
            app:sendMsg("WorldBossController", "trackWorldBoss")
        else
            display.pushToast(L("lua_code_text_249"))
        end
    elseif op == "getGrayBattleTask" then
        -- 接受灰烬入侵任务 TeamTargetVo
        TaskManager:C2S_TaskAcceptGrayBattleRequest(function()
            self:refreshTeam()
        end)
    elseif op == "gotoTraining" then
        -- 打开训练场
        BattleManager:reqTraining(Enums.trainingType.rolePlay)
        app:sendMsg("CosplayController", "setIsReadyIntoCosplay", true)
    end
end

function ClassRef:onOperate(op, ...)
    -- print("TeamMatchController:onOperate op", op, ...)
    if op == "switchPanel" then
        -- 切换面板 idx
        self:switchPanel(...)
    elseif op == "selectTarget" then
        -- 选择目标 TeamTargetVo
        self:selectTarget(...)
    elseif op == "openBook" then
        -- 打开副本详情，一本书 TeamTargetVo
        self:openBook(...)
    elseif op == "openRank" then
        -- 打开排行榜，当前只是竞速副本有 TeamTargetVo
        local teamTarget = ...
        local rankId = teamTarget:getRank()
        UIHandler:handleMessage({id=Constant.FUNC_TYPE_RANK, mode="open", rankingId=rankId})
    elseif op == "createTeam" then
        -- 创建队伍 TeamTargetVo
        self:createTeam(...)
    elseif op == "autoApply" then
        -- 自动申请 TeamTargetVo isAuto
        self._model:C2S_PlayerAutoJoin(...)
    elseif op == "autoRecruit" then
        -- 自动招募 TeamTargetVo isAuto
        self:changeAutoRecruit(...)
    elseif op == "openApply" then
        -- 打开申请列表 弹出标签2-申请标签
        app:sendMsg("NewTeamApplyController", "openView", {2})
    elseif op == "openSet" then
        -- 打开设置界面
        app:sendMsg("NewTeamSettingController", "openView")
    elseif op == "changeGrade" then
        -- 修改组队性质，团队／队伍
        self:changeTeamGrade()
    elseif op == "leaveTeam" then
        -- 退出队伍
        self:leaveTeam()
    elseif op == "MemberInfoPanel_partnerOp" then
        -- 随从操作菜单 position
        if self._model:isMyTeam() then
            local pos, uid = ...
            UIHandler:showHeadMenu( {type=Enums.MenuType.teamPartner, pos=pos, uid=uid} )
        else
            display.pushToast( L("lua_code_text_503") )
        end
    elseif op == "MemberInfoPanel_robotOp" then
        -- 操作机器人
        local pos, baseInfo = ...
        if self._model:isMyTeam() then
            UIHandler:showHeadMenu( {type=Enums.MenuType.teamRobot, pos=pos, attr=baseInfo } )
        end
    elseif op == "teamList_memberOp" then
        -- 组队列表查看别人队伍成员操作(包括机器人)
        local pos, baseInfo = ...
        dump(baseInfo, "baseInfo")
        if baseInfo.uid > 0 then
            local params = {pos=pos, attr=baseInfo, online=true}
            UIHandler:showHeadMenu(params)
        end
    elseif op == "MemberInfoPanel_openInvitePanel" then
        -- 打开队伍邀请面板 弹出标签1-邀请标签
        app:sendMsg("NewTeamApplyController", "openView", { 1 } )
    elseif op == "refreshList" then
        -- 刷新推荐队伍列表 TeamTargetVo
        local teamTarget = ...
        self._model:C2S_GetTeamList(teamTarget.id, true)
    elseif op == "joinTeam" then
        -- 加入队伍 TeamVo
        local team = ...
        local leaderUid = team:getLeaderUid()
        local targetId = team:getTeamTargetId()
        self._model:reqApplyJoinTeam(leaderUid, teagetId, team)
    elseif op == "openOtherSet" then
        -- 打开其他队伍的设置 TeamVo
        local team = ...
        local targetId = team:getTeamTargetId()
        local params = {idx=2, targetId=targetId, team=team}
        app:sendMsg("NewTeamReadyAndSettingController", "openView", params)
    elseif op == "cancelMatch" then
        -- 取消匹配 TeamTargetVo
        self:cancelMatch(...)
    elseif table.hasvalue(START_OPS, op) then
        -- 目标开始操作，包括各种匹配和开始战斗
        self:teamTargetStartOp(op, ...)
    end
end

return ClassRef
